<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="productId">
        <el-select v-model="queryParams.productId" placeholder="请输入产品名称" filterable clearable style="width: 100%;">
          <el-option
            v-for="item in productList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="认证名称" prop="certificationName">
        <el-input
          v-model="queryParams.certificationName"
          placeholder="请输入认证名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认证机构" prop="certificationAuthority">
        <el-input
          v-model="queryParams.certificationAuthority"
          placeholder="请输入认证机构"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['demo:certification:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['demo:certification:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['demo:certification:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['demo:certification:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="certificationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="认证名称" align="center" prop="certificationName" />
      <el-table-column label="认证机构" align="center" prop="certificationAuthority" />
      <el-table-column label="认证编号" align="center" prop="certificationNumber" />
      <el-table-column label="认证日期" align="center" prop="certificationDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.certificationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期至" align="center" prop="expiryDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expiryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容描述" align="center" prop="certificationContent" min-width="120">
        <template slot-scope="scope">
          <el-tooltip 
            v-if="scope.row.certificationContent && scope.row.certificationContent.length > 30" 
            effect="dark" 
            :content="scope.row.certificationContent" 
            placement="top"
            popper-class="custom-tooltip"
          >
            <span>{{ truncateText(scope.row.certificationContent, 30) }}</span>
          </el-tooltip>
          <span v-else>{{ scope.row.certificationContent || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['demo:certification:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['demo:certification:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品权威认证信息抽屉 -->
    <el-drawer :title="title" :visible.sync="open" direction="ltr" size="50%" :before-close="cancel">
      <div class="drawer-content">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-divider content-position="left">基本信息</el-divider>
          <el-form-item label="关联产品" prop="productId">
            <el-select v-model="form.productId" filterable clearable placeholder="请选择关联产品" style="width: 100%;">
              <el-option
                v-for="item in productList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="认证名称" prop="certificationName">
            <el-input v-model="form.certificationName" placeholder="请输入认证名称" />
          </el-form-item>
          <el-form-item label="认证机构" prop="certificationAuthority">
            <el-input v-model="form.certificationAuthority" placeholder="请输入认证机构" />
          </el-form-item>
          <el-form-item label="认证编号" prop="certificationNumber">
            <el-input v-model="form.certificationNumber" placeholder="请输入认证编号" />
          </el-form-item>
          
          <el-divider content-position="left">认证时间</el-divider>
          <el-form-item label="认证日期" prop="certificationDate">
            <el-date-picker clearable
              v-model="form.certificationDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择认证日期"
              style="width: 100%;">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="有效期至" prop="expiryDate">
            <el-date-picker clearable
              v-model="form.expiryDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择有效期至"
              style="width: 100%;">
            </el-date-picker>
          </el-form-item>

          <el-divider content-position="left">认证内容</el-divider>
          <el-form-item label="内容描述" prop="certificationContent">
             <el-input v-model="form.certificationContent" type="textarea" :autosize="{ minRows: 2, maxRows: 8}" placeholder="请输入内容描述" />
          </el-form-item>
          
          <el-divider content-position="left">认证文件</el-divider>
          <el-form-item label="认证文件" prop="certificationFile">
            <file-upload v-model="form.certificationFile" :isDownload="true" />
          </el-form-item>

          <el-divider content-position="left">状态设置</el-divider>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio label="1">启用</el-radio>
              <el-radio label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-divider content-position="left">其他信息</el-divider>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入备注信息" />
          </el-form-item>
        </el-form>
        <div class="drawer-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listCertification, getCertification, delCertification, addCertification, updateCertification } from "@/api/demo/certification";
import { listProduct } from "@/api/demo/product";

export default {
  name: "Certification",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品权威认证信息表格数据
      certificationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productId: null,
        certificationName: null,
        certificationAuthority: null,
        certificationNumber: null,
        certificationDate: null,
        expiryDate: null,
        certificationContent: null,
        certificationFile: null,
        status: null,
      },
      // 表单参数
      form: {
        status: "1" // 默认启用状态
      },
      productList: [],
      // 表单校验
      rules: {
        productId: [
          { required: true, message: "请选择关联产品", trigger: "blur" }
        ],
        certificationName: [
          { required: true, message: "认证名称不能为空", trigger: "blur" }
        ],
        certificationAuthority: [
          { required: true, message: "认证机构不能为空", trigger: "blur" }
        ],
        certificationNumber: [
          { required: true, message: "认证编号不能为空", trigger: "blur" }
        ],
        certificationDate: [
          { required: true, message: "请选择认证日期", trigger: "change" }
        ],
        expiryDate: [
          { required: true, message: "请选择有效期至", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getProductList();
  },
  methods: {
    getProductList() {
      listProduct({pageNum:1,pageSize:1000}).then(response => {
        this.productList = response.rows;
      });
    },
    /** 文字截断方法 */
    truncateText(text, length) {
      if (!text) return '-';
      return text.length > length ? text.substring(0, length) + '...' : text;
    },
    
    handleStatusChange (row) {
      updateCertification({ id: row.id, status: row.status }).then(response => {
        this.$modal.msgSuccess("状态修改成功");
      });
    },
    /** 查询产品权威认证信息列表 */
    getList() {
      this.loading = true;
      listCertification(this.queryParams).then(response => {
        this.certificationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productId: null,
        certificationName: null,
        certificationAuthority: null,
        certificationNumber: null,
        certificationDate: null,
        expiryDate: null,
        certificationContent: null,
        certificationFile: null,
        status: "1", // 重置时默认启用
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getProductList();
      this.reset();
      this.open = true;
      this.title = "添加产品权威认证信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getProductList();
      this.reset();
      const id = row.id || this.ids
      getCertification(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品权威认证信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCertification(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCertification(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除产品权威认证信息编号为"' + ids + '"的数据项？').then(function() {
        return delCertification(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('demo/certification/export', {
        ...this.queryParams
      }, `certification_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.drawer-content {
  padding: 20px;
  height: 100%;
  box-sizing: border-box;
  overflow-y: auto;
}

.drawer-footer {
  position: absolute;
  bottom: 20px;
  right: 20px;
}

.el-divider {
  margin: 30px 0 20px 0;
}

.el-form-item {
  margin-bottom: 20px;
}

/* 为必填项添加红色星号 */
.el-form-item.is-required .el-form-item__label:before {
  content: '*';
  color: #F56C6C;
  margin-right: 4px;
}

/* 表格中截断文本的样式 */
.el-table .cell {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>

<style>
/* 自定义tooltip样式，设置宽度为60% */
.custom-tooltip {
  max-width: 60% !important;
  word-break: break-word;
  white-space: normal;
  line-height: 1.5;
}
</style>